package com.lbn.jfqsc.app.message.action.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.image.LoaderImage;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.DateUtils;
import com.lbn.baseframe.utils.SPUtils;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.alipay.AlipayActivity;
import com.lbn.jfqsc.app.message.action.discuss.DiscussActivity;
import com.lbn.jfqsc.business.bean.ActivityList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class DetailActivity extends BaseActivity<DetailPresenter> implements IDetailView, CompoundButton
        .OnCheckedChangeListener,View.OnClickListener {


    @BindView(R.id.user_photo)
    CircleImageView userPhoto;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.push_time)
    TextView pushTime;
    @BindView(R.id.the_good)
    TextView theGood;
    @BindView(R.id.content_image)
    ImageView contentImage;
    @BindView(R.id.content_text)
    TextView contentText;
    @BindView(R.id.good)
    CheckBox good;
    private ActivityList data;

    private String GOOD_KEY;

    @Override
    protected void onResume() {
        super.onResume();

        if (!App.isMerchant()) {
            return;
        }

        appbar.rightButton("我要赞助", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlipayActivity.startActivity(DetailActivity.this, data.getActId(), null, 2);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail3);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.openBack();
        data = (ActivityList) getIntent().getSerializableExtra("data");
        appbar.setTitle("详情");

        if(data.getActCopy()==null){
            return;
        }

        if(SPUtils.contains(this,data.getActCopy())){
            appbar.rightButton("打开文案",this);
        }else{
            appbar.rightButton("下载活动文案",this);
        }

    }


    @Override
    public void onPresenterCreated() {
        super.onPresenterCreated();
        LoaderImage.displayImage(data.getCommunityLogo(), userPhoto);
        userName.setText(data.getCommunityName());
        pushTime.setText("发布时间 : " + DateUtils.stampToDate(data.getPublishDate()));
        LoaderImage.displayImage(data.getActImg(), contentImage);
        contentText.setText(data.getActContent());
        theGood.setText("点赞数 : " + data.getLikeCount());

        if (App.isLogin()) {
            GOOD_KEY = App.getUser().getId() + "_" + data.getActId();
            if (SPUtils.contains(this, GOOD_KEY)) {
                good.setChecked(true);
            }
        }
        good.setOnCheckedChangeListener(this);


    }

    @Override
    public DetailPresenter createPresenter() {
        return new DetailPresenter();
    }


    public static void startActivity(Context context, ActivityList activity) {
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra("data", activity);
        context.startActivity(intent);
    }

    @OnClick({R.id.good, R.id.look_discuss, R.id.discuss})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.look_discuss:
                DiscussActivity.startActivity(this, data.getActId() + "");
                break;
            case R.id.discuss:
                DiscussActivity.startActivity(this, data.getActId() + "");
                break;
            case R.id.good:
                if (SPUtils.contains(this, GOOD_KEY)) {
                    return;
                }
                mPresenter.good(data.getActId());
                break;
            default:
                mPresenter.checkFile(this,data.getActCopy());
                break;
        }
    }

    @Override
    public void onGoodSucceed(boolean b) {
        good.setChecked(b);
        if (b) {
            theGood.setText("点赞数 : " + (data.getLikeCount() + 1));
            SPUtils.put(this, GOOD_KEY, true);
        }
    }

    @Override
    public void changeDownloadButton() {
        appbar.rightButton("打开文案",this);
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (SPUtils.contains(this, GOOD_KEY)) {
            if (!isChecked) {
                good.setChecked(true);
            }
        }
    }
}
